package com.ja.controller.v1;

import com.alibaba.fastjson.JSON;
import com.ja.domain.Order;
import com.ja.domain.PagingData;
import com.ja.domain.User;
import com.ja.sevice.IOrderService;
import com.ja.sevice.IUserService;
import com.ja.sevice.KongzhiService;
import com.ja.util.DataEncryptUtil;
import com.ja.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/v1/orders")
public class V1PC_OrderController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private KongzhiService kongzhiService;
	
	/**
	 * 查询个人订单-全部-待开奖-已开奖-已中奖-已撤单
	 * @param paging
	 * @param state
	 * @param cname
	 * @returne
	 */
	@RequestMapping(value = "/myOrders", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
	@ResponseBody
	public String myOrders(HttpServletRequest request, PagingData paging, Integer state, String cname) {
		String idStrToken = request.getHeader("Token");
		User user =null;
		if (idStrToken!=null){
			String idStr = null;
			try {
				idStr = new String(DataEncryptUtil.decryptBASE64(idStrToken));
				int id = Integer.parseInt(idStr);
				 user = userService.getUserByid(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(user == null) {
			paging.setReplace1("0");
			return JSON.toJSONString(PagingData.pagingData(paging));
		}
		Order order  = new Order();
		order.setCname(cname);//采种名称
		order.setState(state);//state 1待开奖-2未中奖-3-已中奖-6-和局中奖-7-8-撤单  状态
		order.setUserid(user.getId());//用户id
		paging.setAllCount(orderService.findMyOrdersCounts(order));
		paging.setList(orderService.findMyOrders(paging.getStartIndex(),paging.getLineCount(),user.getId(),state,cname,1));
		return JSON.toJSONString(PagingData.pagingData(paging));
	}
	
	/** 根据条件查询投注记录,需要参数订单id */
	@ResponseBody 
	@RequestMapping(value = "/touzhuRecord", produces = {"application/json;charset=UTF-8"})
	public JsonResult touzhuRecord(Integer id,HttpServletRequest request) {
		String token= request.getHeader("Token");
        User user =null;
        try {
            String idStr = new String(DataEncryptUtil.decryptBASE64(token));
            int userid = Integer.parseInt(idStr);
             user = userService.getUserByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (user==null){
            return new JsonResult("msg","您还没登录，请返回页面就行登录！");
        }
        return new JsonResult("success",orderService.getOneOrder(id));
	}


	/**
	 * 查询订单相关的彩种
	 * @param session
	 * @return
	 */
	@RequestMapping("/myOrderLotters")
	@ResponseBody
	public JsonResult myOrderLotter(HttpSession session) {
		User user = (User) session.getAttribute("user");
		return new JsonResult("success", orderService.fenleiorder(user.getId()));
	}

	/**
	 *   方法名：cancelOrder   
	 *   描述：     彩票订单撤单                  TODO   
	 *   参数：    @param id 订单id
	 *   参数：    @param money 撤单金额
	 *   参数：    @param session
	 *   参数：    @return 
	 * @return: JsonResult
	 */
	@RequestMapping("/chedan")
	@ResponseBody
	public JsonResult chedan(int id, double money, HttpSession session){
		User user = (User) session.getAttribute("user");
		return orderService.cancelOrder(user, id);
	}

	/**
	  * 方法名：calculateBettingCount 
	  * 描述：     用户投注注数                 
	  * 参数：    @param o
	  * 参数：    @return 
	 * @return: JsonResult
	 */
	@RequestMapping(value = "/awards1", produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String calculateBettingCount(Order o,HttpServletRequest request) {
		List<Object> list=new ArrayList<>();
		String token = request.getHeader("Token");
		if(token==null){
			list.add("您还没有登录，请返回登录");
		}

		list.add(orderService.calculateBettingCount(o));
		return JSON.toJSONString(list);
	}
	
	/**
	 *   方法名：awards   
	 *   描述：     用户下注订单                  TODO   
	 *   参数：    @param order 下注信息
	 *   参数：    @param session
	 *   参数：    @return
	 *   参数：    @throws Exception 
	 * @return: JsonResult
	 */
	@RequestMapping(value = "/awards", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
	@ResponseBody
	public JsonResult awards(Order order, HttpServletRequest request){
		String token = request.getHeader("Token");
		System.out.println("awards 的token"+token);
		User user =null;
		try {
			String idStr = new String(DataEncryptUtil.decryptBASE64(token));
			int id = Integer.parseInt(idStr);
			 user = userService.getUserByid(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return orderService.awards(order, user);
		/*	User user = (User) session.getAttribute("user");*/

	}

	/**
	  * 方法名：xiazhugs 
	  * 描述：    查询下注玩法的控制球数                  
	  * 参数：    @return 
	 * @return: JsonResult
	 */
	@ResponseBody
	@RequestMapping("/xiazhugs")
	public JsonResult xiazhugs() {
		return new JsonResult("success", kongzhiService.getAllKongzhi());
	}
}

